﻿namespace EventbriteApi.Model
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    [System.Xml.Serialization.XmlRoot(ElementName = "event", Namespace = "", IsNullable = false)]
    public class Event
    {
        [System.Xml.Serialization.XmlElement("id")]
        public long Id { get; set; }

        [System.Xml.Serialization.XmlElement("title")]
        public string Title { get; set; }

        [System.Xml.Serialization.XmlElement("description")]
        public string Description { get; set; }

        [System.Xml.Serialization.XmlElement("category")]
        public string Category { get; set; }

        [System.Xml.Serialization.XmlElement("tags")]
        public string Tags { get; set; }

        [System.Xml.Serialization.XmlElement("start_date")]
        public string StartDate { get; set; }

        [System.Xml.Serialization.XmlElement("end_date")]
        public string EndDate { get; set; }

        [System.Xml.Serialization.XmlElement("timezone")]
        public string Timezone { get; set; }

        [System.Xml.Serialization.XmlElement("created")]
        public string Created { get; set; }

        [System.Xml.Serialization.XmlElement("modified")]
        public string Modified { get; set; }

        [System.Xml.Serialization.XmlElement("privacy")]
        public string Privacy { get; set; }

        [System.Xml.Serialization.XmlElement("password")]
        public string Password { get; set; }

        [System.Xml.Serialization.XmlElement("capacity")]
        public int Capacity { get; set; }

        [System.Xml.Serialization.XmlElement("url")]
        public string Url { get; set; }

        [System.Xml.Serialization.XmlElement("logo")]
        public string Logo { get; set; }

        [System.Xml.Serialization.XmlElement("status")]
        public string Status { get; set; }

        [System.Xml.Serialization.XmlArrayItem("ticket", IsNullable = true)]
        [System.Xml.Serialization.XmlArray("tickets")]
        public List<Ticket> Tickets { get; set; }

        [System.Xml.Serialization.XmlElement("venue")]
        public Venue Venue { get; set; }

        [System.Xml.Serialization.XmlElement("organizer")]
        public Organizer Organizer { get; set; }

        [System.Xml.Serialization.XmlIgnore()]
        public DateTime StartDateUtc
        {
            get
            {
                return TimeZoneConverter.GetDate(this.StartDate, this.Timezone).ToUniversalTime();
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public DateTime EndDateUtc
        {
            get
            {
                return TimeZoneConverter.GetDate(this.EndDate, this.Timezone).ToUniversalTime();
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public DateTime StartDateLocal
        {
            get
            {
                return TimeZoneConverter.GetDate(this.StartDate, this.Timezone).ToLocalTime();
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public DateTime EndDateLocal
        {
            get
            {
                return TimeZoneConverter.GetDate(this.EndDate, this.Timezone).ToLocalTime();
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public int VisibleTickets
        {
            get
            {
                if (this.Tickets == null)
                {
                    return 0;
                }
                else
                {
                    return this.Tickets.Count(t => t.Visible);
                }
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public int TicketsSold
        {
            get
            {
                if (this.Tickets == null)
                {
                    return 0;
                }
                else
                {
                    return this.Tickets.Sum(t => t.QuantitySold);
                }                
            }
        }

        [System.Xml.Serialization.XmlIgnore()]
        public int TicketsAvailable
        {
            get
            {
                if (this.Tickets == null)
                {
                    return 0;
                }
                else
                {
                    return this.Tickets.Sum(t => t.QuantityAvailable);
                }
            }
        }
    }
}
